package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.bean.LoginInfoBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.AppUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IUserInfoView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/5/21.
 */

public class UserInfoPresenter extends BasePresenter<IUserInfoView> {

    public UserInfoPresenter(IUserInfoView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void uploadAvatar(String token, String path) {
        iView.showLoadingDialog();
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        if (token != null)
            builder.addFormDataPart("token", token);
        RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        builder.addFormDataPart("file", file.getName(), photoRequestBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).uploadAvatar(parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.uploadAvatarSuccess(response.getData());
                    }
                }));
    }

    private void getUserInfo(String token) {
        addSubscription(RetrofitSource.createApi(HttpPostService.class).user_list(token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<LoginInfoBean>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<LoginInfoBean> response) {
                        //更新资料成功后获取最新资料并保存到本地
                        LoginInfoBean data = response.getData();
                        data.setToken(token);
                        SharedPreferencesUtil.getsInstances(AppUtils.getContext()).setLoginInfo(data);
                        iView.updateSuccess();
                    }
                }));
    }

    public void updateUserInfo(String token, String pic, String nickName, String sex,
                               String age, String province, String city, String area, String signature) {
        iView.showLoadingDialog();
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        if (pic != null)
            map.put("pic", pic);
        map.put("user_nicename", nickName);
        map.put("sex", sex);
        if (age == null) {
            map.put("age", "1");
        } else {
            map.put("age", age);
        }
        if (province != null) {
            map.put("province", province);
            map.put("city", city);
            map.put("area", area);
        }
        if (signature != null)
            map.put("signature", signature);
        addSubscription(RetrofitSource.createApi(HttpPostService.class).upd_date(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        getUserInfo(token);
                    }
                }));
    }
}
